package com.hg.oa;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.backends.okhttp3.OkHttpImagePipelineConfigFactory;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.hg.oa.common.error.ErrorPageTaskQueue;
import com.hg.oa.common.restful.RestClient;
import com.hg.oa.common.wifiupload.UploadTask;
import com.hg.oa.common.wifiupload.UploadTaskQueue;
import com.hg.oa.util.FileUtils;
import com.hg.oa.util.LocationUtils;

import java.util.concurrent.BlockingQueue;


public class MyApplication extends Application {

    String tag = "MyApplication";

    @Override
    public void onCreate() {
        super.onCreate();
        String processName = getCurProcessName(this);
        Log.e(tag, "processName-->" + processName);
        if (processName.equals("com.hg.oa")) {
            Log.i("applicayion", "onCreate");
            LocationUtils.init(this);
            RestClient.init(this);
            ImagePipelineConfig config = OkHttpImagePipelineConfigFactory
                    .newBuilder(this, RestClient.getOkHttpClient())
                    .build();
            Fresco.initialize(this, config);
            ErrorPageTaskQueue.init(this);

            BlockingQueue<UploadTask> queue = FileUtils.init(this);
            if (queue != null) {
                UploadTaskQueue.queue = queue;
                Log.e(tag, "UploadTaskQueue.queue size-->" + UploadTaskQueue.queue.size());
            }
        }

    }


    String getCurProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager mActivityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager
                .getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }

}
